local zhuotu = fk.CreateSkill{
  name = "xiaobai__zhuotu",
  tags = {Skill.Compulsory}
}

zhuotu:addEffect(fk.EventPhaseProceeding, {
  anim_type = "special",
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(self) and player.phase == Player.Discard
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    room:setPlayerMark(player, "@@xiaobai__zhuotu", 1)
    local n = #table.filter(room.alive_players, function (p)
      return p:getHandcardNum() == 4
    end)
    if n > 0 and not player:isNude() then
      n = n * n
      room:askToDiscard(player, {
        max_num = n, 
        min_num = n, 
        include_equip = true, 
        skill_name = zhuotu.name, 
        cancelable = false})
    end
    data.phase_end = true
  end,
})

zhuotu:addEffect(fk.TurnStart, {
  can_refresh = function (self, event, target, player, data)
    return target == player and player:getMark("@@xiaobai__zhuotu") > 0
  end,
  on_refresh = function (self, event, target, player, data)
    player.room:setPlayerMark(player, "@@xiaobai__zhuotu", 0)
  end,
})

zhuotu:addEffect(fk.AfterCardsMove, {
  mute = true,
  can_trigger = function(self, event, target, player, data)
    if player:getMark("@@xiaobai__zhuotu") > 0 and player:getHandcardNum() ~= 4 then
      for _, move in ipairs(data) do
        if move.from == player then
          for _, info in ipairs(move.moveInfo) do
            if info.fromArea == Card.PlayerHand then
              return true
            end
          end
        end
        if move.to == player and move.toArea == Card.PlayerHand then
          return true
        end
      end
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    player:broadcastSkillInvoke("xiaobai__zhuotu")
    local n = player:getHandcardNum() - 4
    if n < 0 then
      room:notifySkillInvoked(player, "xiaobai__zhuotu", "drawcard")
      player:drawCards(-n, "xiaobai__zhuotu")
    else
      room:notifySkillInvoked(player, "xiaobai__zhuotu", "negative")
      room:askToDiscard(player, {
        max_num = n,
        min_num = n,
        include_equip = false,
        cancelable = false,
        skill_name = zhuotu.name
      })
    end
  end,
})

Fk:loadTranslationTable{
  ["xiaobai__zhuotu"] = "琢图",
  [":xiaobai__zhuotu"] = "锁定技，你的弃牌阶段改为弃置X²张牌（X为场上手牌数为4的角色数，不足则全弃），直到你下个回合开始，"..
  "当你失去或获得手牌后，你将手牌数调整为4。",
  ["@@xiaobai__zhuotu"] = "琢图",
  ["#xiaobai__zhuotu_delay"] = "琢图",
  ["$xiaobai__zhuotu1"] = "勾、股相乘为朱实二，倍之为朱实四。",
  ["$xiaobai__zhuotu2"] = "以形验数、形数为一，先贤之理可证矣。",
}
return zhuotu